capture program drop kl_binary
program define kl_binary

syntax [if] [in], ind_var(string) treatment(string) gen(string) prefix(integer) ///
				  sd(real)

	local sd1 = round(`sd', 0.01)

	display "Independent variable is `ind_var'"
	display "Treatment variable is `treatment'"
	display "New variable to be generated is `gen'`prefix'"
	display "standard deviation specified is `sd1'"


//generating the new variable
	gen `gen'`prefix' = `ind_var'  
	label variable `gen'`prefix' "`ind_var' Imputation using `sd1' standard deviation"
	replace `gen'`prefix'=0 if `ind_var' ==.

**generate a random number to sort data before imputation
	gen random = runiform()

**generate a dummy variable to identify missing enroll 
	gen d_imp = 0
	replace d_imp = 1 if `ind_var' ==.

**generate a ranking variable that will be used for imputation

	bysort `treatment' d_imp (random): gen rank = _n


***Control group
**calculate mean and sd
	summ `ind_var' if `treatment'==0
	local sd_c = `r(sd)'*`sd1'  //
	local mean_c = `r(mean)'     // mean

	display `sd_c'
	display `mean_c' 

	local impu_c = `mean_c' + `sd_c'


**Calculate number of missing values control
	count if `ind_var'==. & `treatment'==0
	local missing_c = `r(N)'

**calculate the number of obs that should be 1 so that the mean of control imp 
** is 
	local number_imp_1_c = round(`missing_c' * `impu_c')
	display `number_imp_1_c'

	replace `gen'`prefix'=1 if 	`treatment'==0 & rank<=`number_imp_1_c' & `ind_var'==.


***Treatment group
**calculate mean and sd
	summ `ind_var' if `treatment'==1
	local sd_t = `r(sd)'*`sd1'  //
	local mean_t = `r(mean)'     // mean

	display `sd_t'
	display `mean_t' 

	local impu_t = `mean_t' - `sd_t'



**Calculate number of missing values control
	count if `ind_var'==. & `treatment'==1
	local missing_t = `r(N)'

**calculate the number of obs that should be 1 so that the mean of control imp 
** is 

	local number_imp_1_t = round(`missing_t' * `impu_t')
	display `number_imp_1_t'

	replace `gen'`prefix'=1 if 	`treatment'==1 & rank<=`number_imp_1_t' & `ind_var'==.


	drop random d_imp rank

end 